/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DigaFoil;

import BD.*;
import GUI.Gui;
import java.util.List;
import javax.persistence.*;
import javax.swing.JOptionPane;

/**
 *
 * @author ARMGARCES
 */
public class Log {
    private static final String PERSISTENCE_UNIT_NAME = "DigaFoilPU";
    
    private Log() {
    }
    
    public static Log getInstance() {
        return LogHolder.INSTANCE;
    }
    
    private static class LogHolder {

        private static final Log INSTANCE = new Log();
    }
    
    /**
     * 
     * @return 
     */
    public boolean iniciarSesion(){        
        return crearBitacora(Controlador.getInstance().getUser(),"Iniciar Sesion","");
    }
    
    /**
     * 
     * @return 
     */
    public boolean cerrarSesion(){        
        return crearBitacora(Controlador.getInstance().getUser(),"Cerrar Sesion","");
    }
    
    /**
     * 
     */
    protected void sesionDeshabilitada() {
        crearBitacora(Controlador.getInstance().getUser(),"Sesion Deshabilitada","");
    }
    /**
     * 
     * @param _user
     * @return 
     */
    public boolean addUsuario(Usuario _user){        
        return crearBitacora(Controlador.getInstance().getUser(),"Agregar Usuario",_user.getNombre());
    }
    /**
     * 
     * @param _lugar 
     * @return 
     */
    public boolean addLugar(Lugar _lugar){        
        return crearBitacora(Controlador.getInstance().getUser(),"Agregar Lugar",_lugar.getNombre());
    }
    /**
     * 
     * @param _obj
     * @return 
     */
    public boolean addProveedor(Proveedor _obj){        
        return crearBitacora(Controlador.getInstance().getUser(),"Agregar Proveedor",_obj.getNombre());
    }
    /**
     * 
     * @param _user
     * @param desc
     * @return 
     */
    private boolean crearBitacora(Usuario _user,String desc,String param){
        boolean result = false;
        try{
          if(_user != null){
            EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
            EntityManager em = factory.createEntityManager();

            //use entity manager to retrieve named query
            Query findAccion = em.createNamedQuery("Acciones.findByDescripcion");
            //set dynamic data for query
            findAccion.setParameter("descripcion",desc);
            
            //execute query and get results
            List<Acciones> acciones = findAccion.getResultList();
            
            if(acciones.size()>0){
                em.getTransaction().begin();
                
                Acciones accion = acciones.get(0);
                
                Bitacora log = new Bitacora();
                log.setFkidAcciones(accion);
                log.setFkidUsuario(_user);
                log.setFecha(Tools.getDate());
                log.setIp(Tools.getIpAddress());
                log.setMac(Tools.getMacAddress());
                log.setMensj(param);
                //em.persist(log);
                em.getTransaction().commit();
                em.close();
                result = true;
            }else{
                String mnsj  = "La Accion no existe!";
                String title = "Error en Bitacora";
                JOptionPane.showMessageDialog(Gui.getInstance().getPantalla(),mnsj,title, JOptionPane.ERROR_MESSAGE);
                result = false;
            }
          }            
        }catch(PersistenceException pe){
            String mnsj  = "Error de Sistema, por Favor Intente Luego!";
            String title = "Error de Sistema";
            JOptionPane.showMessageDialog(Gui.getInstance().getPantalla(),mnsj,title, JOptionPane.ERROR_MESSAGE);            
            pe.printStackTrace();
            result = false;
        }catch(Exception e){
            String mnsj  = "Error Inesperado, por Favor Intente Luego!";
            String title = "Error de Sistema";
            JOptionPane.showMessageDialog(Gui.getInstance().getPantalla(),mnsj,title, JOptionPane.ERROR_MESSAGE);            
            result = false;
            e.printStackTrace();
        }
        return result;
    }
}
